﻿#ifndef SIGNALTRANSFER_H
#define SIGNALTRANSFER_H

#include <QObject>
#include <QSettings>
#include <QSoundEffect>

class QMediaPlayer;

#define ST SignalTransfer::instance()

/********************************************************
 * @ClassName:  SignalTransfer
 * @Brief:      信号中继类，单例模式
 * @date        2022/01/19
 ********************************************************/
class SignalTransfer : public QObject
{
    Q_OBJECT
public:
    static SignalTransfer *instance();
    static void closeInstance();

    struct SoundList {
        QSoundEffect freeze;
        QSoundEffect gameover;
        QSoundEffect down;
        QSoundEffect remove;
        QSoundEffect success;
        QSoundEffect kickfailure;
        QSoundEffect kicksuccess;

        void setVolume(qreal volume){
            qDebug()<<"设置音效音量："<<volume;
            freeze.setVolume(volume);
            gameover.setVolume(volume);
            down.setVolume(volume);
            remove.setVolume(volume);
            success.setVolume(volume);
            kickfailure.setVolume(volume);
            kicksuccess.setVolume(volume);
        };
    };

    void delay_ms(int ms);    // 延迟

    QSettings *settings() const;

    int blockSideLength() const;
    void setBlockSideLength(int newBlockSideLength);

    void setBackgroundMusicEnable(bool flag);       //设置背景音乐开关

    QMediaPlayer *backgroundplayer() const;

    void insertStandardModeRecord(const Record &r);
    void insertPassModeRecord(const Record &r);

    QList<Record> standard_record_list() const;
    bool canRankInStandardRecord(int score);    // 查询能否上标准榜

    QList<Record> pass_record_list() const;
    bool canRankInPassRecord(const Record &record);    // 查询能否上闯关榜

    int speedToInterval(int speed);      //速度转成间隔

    SoundList *soundList() const;

signals:

private:
    explicit SignalTransfer(QObject *parent = nullptr);
    virtual ~SignalTransfer();
    static SignalTransfer *m_pInstance;

    QSettings *m_settings;                   // 设置
    QMediaPlayer *m_backgroundplayer;        // 播放器
    SoundList *m_soundList;                  // 声音列表
    QList<Record> m_standard_record_list;    // 标准模式得分榜
    QList<Record> m_pass_record_list;        // 闯关模式得分榜
    int m_blockSideLength;                   // 边长
    QMap<int, double> m_speedTable;          // 速度表
    const int maxRecordNum = 15;             // 记录长度

    static bool compareByScore(const Record &a, const Record &b) {
        return a.score > b.score;
    }

    static bool compareByPlaytime(const Record &a, const Record &b) {
        return a.play_time < b.play_time;
    }

    static bool compareInPassMode(const Record &a, const Record &b) {
        if (a.pass_difficulty == b.pass_difficulty) {
            return a.play_time < b.play_time;
        } else {
            return a.pass_difficulty > b.pass_difficulty;
        }
    }

};

#endif // SIGNALTRANSFER_H
